package com.specialtreasure.freightdepot.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.core.util.CommonUtil;
import com.app.core.util.DateUtils;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.model.ItemBean;
import com.specialtreasure.freightdepot.model.ZUserResponse;
import com.vondear.rxtools.RxAppUtils;
import com.vondear.rxtools.RxDeviceUtils;
import com.vondear.rxtools.RxPhotoUtils;
import com.vondear.rxtools.RxUtils;
import com.vondear.rxtools.view.dialog.RxDialogLoading;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/22 0022.
 * 货源信息适配器
 */

public class GoodsInfoAdapter extends BaseAdapter {
    List<ItemBean> itemBeans = new ArrayList<ItemBean>();
    Context context;

    public GoodsInfoAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (itemBeans != null && itemBeans.size() > 0) {
            return itemBeans.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return itemBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (null == view) {
            view = View.inflate(viewGroup.getContext(), R.layout.item_goods_info_list, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final ItemBean itemBean = (ItemBean) getItem(i);
        //设置信息
        if (itemBean != null) {
            ZUserResponse zUserResponse = itemBean.getzUserResponse();
            if (zUserResponse != null) {
                viewHolder.tv_name.setText(CommonUtil.checkString(zUserResponse.getRealName()));
            }
            viewHolder.tv_car_address.setText(CommonUtil.checkString(itemBean.getContent()));
            viewHolder.tv_car_date.setText(DateUtils.timeMinute(itemBean.getPushTime()));


            //打电话
            viewHolder.iv_telphone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + itemBean.getPhone()));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);

                }
            });

        }

        return view;
    }


    public static class ViewHolder {
        public View rootView;
        public TextView tv_name;
        public TextView tv_car_date;
        public TextView tv_car_address;
        public TextView tv_car_height;
        public LinearLayout ll_address;
        public ImageView iv_telphone;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_name = (TextView) rootView.findViewById(R.id.tv_name);
            this.tv_car_date = (TextView) rootView.findViewById(R.id.tv_car_date);
            this.tv_car_address = (TextView) rootView.findViewById(R.id.tv_car_address);
            this.tv_car_height = (TextView) rootView.findViewById(R.id.tv_car_height);
            this.ll_address = (LinearLayout) rootView.findViewById(R.id.ll_address);
            this.iv_telphone = (ImageView) rootView.findViewById(R.id.iv_telphone);
        }

    }


    public void setItemBeans(List<ItemBean> itemBeans) {
        this.itemBeans = itemBeans;
    }

    public void addItem(ItemBean itemBean) {
        if (itemBeans != null) {
            itemBeans.add(0, itemBean);
            notifyDataSetChanged();
        }
    }

    public void delItem(Integer itemId) {
        if (itemBeans != null) {
            for (int i = 0; i < itemBeans.size(); i++) {
                ItemBean tempItemBean = itemBeans.get(i);
                if (itemId.equals(tempItemBean.getItemId())) {
                    itemBeans.remove(tempItemBean);
                    notifyDataSetChanged();
                    break;
                }
            }
        }

    }


}
